package com.playplus.utils.push
{
	import flash.events.RemoteNotificationEvent;
	import flash.events.StatusEvent;
	import flash.notifications.NotificationStyle;
	import flash.notifications.RemoteNotifier;
	import flash.notifications.RemoteNotifierSubscribeOptions;

	public class PushNotifiManager
	{
		
		private var preferredStyles:Vector.<String>;
		private var subscribeOptions:RemoteNotifierSubscribeOptions;
		private var remoteNot:RemoteNotifier;
		
		
		public function PushNotifiManager()
		{
			preferredStyles 	= new Vector.<String>();
			subscribeOptions 	= new RemoteNotifierSubscribeOptions();
			remoteNot 			= new RemoteNotifier();
		}
		
		public function init():void
		{
			preferredStyles.push(NotificationStyle.ALERT ,NotificationStyle.BADGE,NotificationStyle.SOUND );
			subscribeOptions.notificationStyles= preferredStyles;
			
			initEvent();
		}
		
		public function getToken():void
		{
			if(RemoteNotifier.supportedNotificationStyles.toString() != " ") {	
				remoteNot.subscribe(subscribeOptions);
			}
			else{
				//tt.appendText("\n Remote Notifications not supported on this Platform !");
			}
		}
		
		protected function initEvent():void
		{
			remoteNot.addEventListener(RemoteNotificationEvent.TOKEN,tokenHandler);
			remoteNot.addEventListener(RemoteNotificationEvent.NOTIFICATION,notificationHandler);
			remoteNot.addEventListener(StatusEvent.STATUS,statusHandler);
		}
		
		protected function removeEvent():void
		{
			remoteNot.removeEventListener(RemoteNotificationEvent.TOKEN,tokenHandler);
			remoteNot.removeEventListener(RemoteNotificationEvent.NOTIFICATION,notificationHandler);
			remoteNot.removeEventListener(StatusEvent.STATUS,statusHandler);
		}
		
		
		private function tokenHandler(e:RemoteNotificationEvent):void{
			trace("\nRemoteNotificationEvent type: "+e.type +"\ntokenID:\n"+ e.tokenId);
		}
		
		private function notificationHandler(e:RemoteNotificationEvent):void{
			//tt.appendText("\nRemoteNotificationEvent type: " + e.type +"\nbubbles: "+ e.bubbles + "\ncancelable " +e.cancelable);
			
			for (var x:String in e.data) {
				//tt.text += "\n"+ x + ":  " + e.data[x];
			}			
		}
		private function statusHandler(e:StatusEvent):void{
			trace("event Level" + e.level +"\nevent code " + e.code + "\ne.currentTarget: " + e.currentTarget.toString());
		}
	}
}